package myThread;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * User: cool
 * Date: 2018/4/2
 * Time: 10:29
 */
public class myThread {

    private int i = 0;


    public String getnums(){

        StringBuffer nums = new StringBuffer();

       //synchronized (this){
            nums.append(""+i++);
            //System.out.println(i);

        //}

        return nums.toString();
    }

     static class Task implements Callable {

        private String id;
        Task(String id){
            this.id = id;
        }

         @Override
         public Object call() throws Exception {
             myThread myThread = new myThread();
             String nums = myThread.getnums();

             System.out.println(Thread.currentThread().getName()+"运行"+nums);
             //Thread.sleep(1000);
             return nums;
         }
     }


    public static void main(String[] args){

        ExecutorService cachedThreadPool = Executors.newCachedThreadPool();

        for(int i=0;i<100;i++){
            Callable a = new Task(i+"");
            cachedThreadPool.submit(a);
        }
/*
        Callable a = new Task("a");
        Callable b = new Task("b");
        Callable c = new Task("b");
        Callable d = new Task("b");
        Callable e = new Task("b");

        // 执行任务并获取Future对象
        Future f1 = cachedThreadPool.submit(a);
        Future f2 = cachedThreadPool.submit(b);
        Future f3 = cachedThreadPool.submit(c);
        Future f4 = cachedThreadPool.submit(d);
        Future f5 = cachedThreadPool.submit(e);*/

        // 从Future对象上获取任务的返回值，并输出到控制台
       /* try {
            System.out.println(">>>" + f1.get().toString());
            System.out.println(">>>" + f2.get().toString());
            System.out.println(">>>" + f3.get().toString());
            System.out.println(">>>" + f4.get().toString());
            System.out.println(">>>" + f5.get().toString());
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        } catch (ExecutionException e1) {
            e1.printStackTrace();
        }*/


        cachedThreadPool.shutdown();
    }
}
